package com.yuncheng.spcyApi.utils;



import java.util.*;

public class SortUtils {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("桂食药审查药生函〔2023〕1号");
        list.add("桂食药审查药生函〔2023〕2号");
        list.add("桂食药审查药生函〔2023〕10号");
        list.add("桂食药审查药生函〔2023〕5号");

        String[] s = list.toArray(new String[list.size()]);
        System.out.printf("原排序：%s%n", Arrays.toString(s));

        /*Arrays.sort(s, (a, b) -> b.compareTo(a));
        System.out.printf("降序排序：%s%n", Arrays.toString(s));*/

        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return SortUtils.extractInt(o2) - SortUtils.extractInt(o1);
            }
        });

        System.out.println(list);

        /*SortUtils.quickSort(s,0,list.size()-1);
        System.out.println("------------------------------------");
        System.out.printf("排序后：%s%n", Arrays.toString(s));*/
    }

    /**
     * 包含数字的字符串-降序排序
     * @param list
     */
    public static void StringNumberSortDesc(List<String> list){
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return SortUtils.extractInt(o2) - SortUtils.extractInt(o1);
            }
        });
    }

    public static int extractInt(String s){
        String num = s.replaceAll("\\D","");
        return num.isEmpty() ? 0 : Integer.parseInt(num);
    }

    public static void quickSort(String[] arr,int left,int right)            //快速排序算法
    {
        String  f,t;
        int rtemp,ltemp;

        ltemp=left;
        rtemp=right;
        f=arr[(left+right)/2];                        //分界值
        while(ltemp<rtemp)
        {
            while(arr[ltemp].compareTo(f)<0)
            {
                ++ltemp;
            }
            while(arr[rtemp].compareTo(f)>0)
            {
                --rtemp;
            }
            if(ltemp<=rtemp)
            {
                t=arr[ltemp];
                arr[ltemp]=arr[rtemp];
                arr[rtemp]=t;
                --rtemp;
                ++ltemp;
            }
        }
        if(ltemp==rtemp)
        {
            ltemp++;
        }
        if(left<rtemp)
        {
            quickSort(arr,left,ltemp-1);            //递归调用
        }
        if(ltemp<right)
        {
            quickSort(arr,rtemp+1,right);            //递归调用
        }
    }

    /**
     * 使用TreeSet实现List去重(有序)T
     */
    public static <T> List<T> removeDuplicationByTreeSet(List<T> list){
        TreeSet treeSet = new TreeSet(list);
        list.clear();
        list.addAll(treeSet);
        return list;
    }
}
